{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# azure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[{\"faceId\": \"1fadc823-53d4-47e3-8eaa-8b47bb5e7d26\", \"faceRectangle\": {\"top\": 189, \"left\": 225, \"width\": 207, \"height\": 207}, \"faceAttributes\": {\"smile\": 0.207, \"headPose\": {\"pitch\": -4.3, \"roll\": -2.2, \"yaw\": -18.1}, \"gender\": \"male\", \"age\": 5.0, \"facialHair\": {\"moustache\": 0.0, \"beard\": 0.0, \"sideburns\": 0.0}, \"glasses\": \"NoGlasses\", \"emotion\": {\"anger\": 0.0, \"contempt\": 0.001, \"disgust\": 0.0, \"fear\": 0.0, \"happiness\": 0.207, \"neutral\": 0.79, \"sadness\": 0.0, \"surprise\": 0.002}, \"blur\": {\"blurLevel\": \"medium\", \"value\": 0.43}, \"exposure\": {\"exposureLevel\": \"goodExposure\", \"value\": 0.63}, \"noise\": {\"noiseLevel\": \"medium\", \"value\": 0.65}, \"makeup\": {\"eyeMakeup\": false, \"lipMakeup\": false}, \"accessories\": [], \"occlusion\": {\"foreheadOccluded\": false, \"eyeOccluded\": false, \"mouthOccluded\": false}, \"hair\": {\"bald\": 0.07, \"invisible\": false, \"hairColor\": [{\"color\": \"brown\", \"confidence\": 0.91}, {\"color\": \"black\", \"confidence\": 0.87}, {\"color\": \"blond\", \"confidence\": 0.59}, {\"color\": \"gray\", \"confidence\": 0.3}, {\"color\": \"other\", \"confidence\": 0.18}, {\"color\": \"red\", \"confidence\": 0.09}, {\"color\": \"white\", \"confidence\": 0.0}]}}}]'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-1 面部检测\n",
    "import requests\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = \"0506b6e2cd2041d88e0993544d600153\"\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-jimmy.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文body\n",
    "image_url = 'https://gitee.com/ljwennn/api-job/raw/master/image/wulei.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数parameters\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->bytes\n",
    "json.dumps(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bytes or string   编码不同  *****  encoding =\"utf-8\"\n",
    "# response.text\n",
    "# response.content\n",
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '1fadc823-53d4-47e3-8eaa-8b47bb5e7d26',\n",
       "  'faceRectangle': {'top': 189, 'left': 225, 'width': 207, 'height': 207},\n",
       "  'faceAttributes': {'smile': 0.207,\n",
       "   'headPose': {'pitch': -4.3, 'roll': -2.2, 'yaw': -18.1},\n",
       "   'gender': 'male',\n",
       "   'age': 5.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.207,\n",
       "    'neutral': 0.79,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.002},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.43},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.63},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.65},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.07,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.91},\n",
       "     {'color': 'black', 'confidence': 0.87},\n",
       "     {'color': 'blond', 'confidence': 0.59},\n",
       "     {'color': 'gray', 'confidence': 0.3},\n",
       "     {'color': 'other', 'confidence': 0.18},\n",
       "     {'color': 'red', 'confidence': 0.09},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '1fadc823-53d4-47e3-8eaa-8b47bb5e7d26',\n",
       "  'faceRectangle': {'top': 189, 'left': 225, 'width': 207, 'height': 207},\n",
       "  'faceAttributes': {'smile': 0.207,\n",
       "   'headPose': {'pitch': -4.3, 'roll': -2.2, 'yaw': -18.1},\n",
       "   'gender': 'male',\n",
       "   'age': 5.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.001,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.207,\n",
       "    'neutral': 0.79,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.002},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.43},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.63},\n",
       "   'noise': {'noiseLevel': 'medium', 'value': 0.65},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.07,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'brown', 'confidence': 0.91},\n",
       "     {'color': 'black', 'confidence': 0.87},\n",
       "     {'color': 'blond', 'confidence': 0.59},\n",
       "     {'color': 'gray', 'confidence': 0.3},\n",
       "     {'color': 'other', 'confidence': 0.18},\n",
       "     {'color': 'red', 'confidence': 0.09},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-2\n",
    "results = response.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_face = pd.json_normalize(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1fadc823-53d4-47e3-8eaa-8b47bb5e7d26']"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceID = df_face['faceId'].values.tolist()\n",
    "faceID "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['faceId', 'faceRectangle.top', 'faceRectangle.left',\n",
       "       'faceRectangle.width', 'faceRectangle.height', 'faceAttributes.smile',\n",
       "       'faceAttributes.headPose.pitch', 'faceAttributes.headPose.roll',\n",
       "       'faceAttributes.headPose.yaw', 'faceAttributes.gender',\n",
       "       'faceAttributes.age', 'faceAttributes.facialHair.moustache',\n",
       "       'faceAttributes.facialHair.beard',\n",
       "       'faceAttributes.facialHair.sideburns', 'faceAttributes.glasses',\n",
       "       'faceAttributes.emotion.anger', 'faceAttributes.emotion.contempt',\n",
       "       'faceAttributes.emotion.disgust', 'faceAttributes.emotion.fear',\n",
       "       'faceAttributes.emotion.happiness', 'faceAttributes.emotion.neutral',\n",
       "       'faceAttributes.emotion.sadness', 'faceAttributes.emotion.surprise',\n",
       "       'faceAttributes.blur.blurLevel', 'faceAttributes.blur.value',\n",
       "       'faceAttributes.exposure.exposureLevel',\n",
       "       'faceAttributes.exposure.value', 'faceAttributes.noise.noiseLevel',\n",
       "       'faceAttributes.noise.value', 'faceAttributes.makeup.eyeMakeup',\n",
       "       'faceAttributes.makeup.lipMakeup', 'faceAttributes.accessories',\n",
       "       'faceAttributes.occlusion.foreheadOccluded',\n",
       "       'faceAttributes.occlusion.eyeOccluded',\n",
       "       'faceAttributes.occlusion.mouthOccluded', 'faceAttributes.hair.bald',\n",
       "       'faceAttributes.hair.invisible', 'faceAttributes.hair.hairColor'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_face.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1fadc823-53d4-47e3-8eaa-8b47bb5e7d26</td>\n",
       "      <td>NoGlasses</td>\n",
       "      <td>0.79</td>\n",
       "      <td>5.0</td>\n",
       "      <td>male</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId faceAttributes.glasses  \\\n",
       "0  1fadc823-53d4-47e3-8eaa-8b47bb5e7d26              NoGlasses   \n",
       "\n",
       "   faceAttributes.emotion.neutral  faceAttributes.age faceAttributes.gender  \n",
       "0                            0.79                 5.0                  male  "
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_face[['faceId','faceAttributes.glasses','faceAttributes.emotion.neutral','faceAttributes.age','faceAttributes.gender']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Face++"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Face++\n",
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "# 2、输入我们API_Key/API_secret\n",
    "api_key = 'KDKmcYsLUL0yfAIZscmjQVCuiXlQUGUZ'\n",
    "\n",
    "api_secret = '6w_OqzKm3NrKYA9y34_ojxpnSdc3vntz'\n",
    "\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect'  # 请求URL 即平台\n",
    "img_url1 = 'https://gitee.com/ljwennn/api-job/raw/master/image/wulei.jpg'\n",
    "# 4、头部headers\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、调用参数\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url1,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603522736,dfdc6aae-3cbc-42f8-aa5d-79dbacbb4469\",\"time_used\":519,\"faces\":[{\"face_token\":\"c492d26c35f0986dd85befc214ffb7f9\",\"face_rectangle\":{\"top\":190,\"left\":248,\"width\":227,\"height\":227},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":9},\"smile\":{\"value\":29.594,\"threshold\":50.000},\"emotion\":{\"anger\":0.011,\"disgust\":0.011,\"fear\":0.011,\"happiness\":95.777,\"neutral\":3.905,\"sadness\":0.038,\"surprise\":0.246}}}],\"image_id\":\"4HoXk0YoGEvU2I0H1wTlAw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603522736,dfdc6aae-3cbc-42f8-aa5d-79dbacbb4469',\n",
       " 'time_used': 519,\n",
       " 'faces': [{'face_token': 'c492d26c35f0986dd85befc214ffb7f9',\n",
       "   'face_rectangle': {'top': 190, 'left': 248, 'width': 227, 'height': 227},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 9},\n",
       "    'smile': {'value': 29.594, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.011,\n",
       "     'disgust': 0.011,\n",
       "     'fear': 0.011,\n",
       "     'happiness': 95.777,\n",
       "     'neutral': 3.905,\n",
       "     'sadness': 0.038,\n",
       "     'surprise': 0.246}}}],\n",
       " 'image_id': '4HoXk0YoGEvU2I0H1wTlAw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results = r.json() # \n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Baidu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.ad14da3bdb42fb82f75ec85bf18f09e4.315360000.1918882186.282335-22869214', 'expires_in': 2592000, 'session_key': '9mzdCuU0fCF9ZpY8kUcGIOQVX+rZa6GGAc62DSkv2UbRn0ZyU18uHNwtjQDwMCYvfHTOwH2t/PPrtI3eIDD0HpVsQcUgBw==', 'access_token': '24.1a7765d9ddd4976c20ad7a8f3d1349a5.2592000.1606114186.282335-22869214', 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component', 'session_secret': '958287902f80149a235606b9d3187aef'}\n"
     ]
    }
   ],
   "source": [
    "# BAIDU\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=NIbY5Nwjxkk3DGrnWGpGQUt8&client_secret=ZFBPyxTpa2WozwjKPhgWWM1mG9Bo9uKc'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 6520189757500, 'timestamp': 1603522305, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': '33e38b2f65a40e14cc1ae2d52504981c', 'location': {'left': 240.64, 'top': 204.63, 'width': 237, 'height': 216, 'rotation': -3}, 'face_probability': 1, 'angle': {'yaw': 22.15, 'pitch': 13.88, 'roll': -6.83}, 'face_shape': {'type': 'round', 'probability': 0.7}, 'face_type': {'type': 'human', 'probability': 0.97}}]}}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸检测与属性分析\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://gitee.com/ljwennn/api-job/raw/master/image/wulei.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "access_token = '24.ea61c5014dd20ce48ec03b0a731b80fd.2592000.1606114139.282335-22869214'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
